/**
 * Created by huangying on 16/11/10.
 */
var keyword = "";
var _text = "";
$(document).ready(function () {
    keyword = location.search; //获取url中"?"符后的字串
    if (keyword != '') {
        keyword = keyword.substring(9, keyword.length);
        keyword = decodeURI(keyword);//中文解码
        $('.search-input #keyword').val(keyword);
        _text = "内容";
        data_motocycle = {
            page: page,
            keywords: keyword,
            rows: 20
        };
        searchkey_content();
        page_show();
    }
});


//选择内容或用户搜索
$(document).on('click', '.search-btn-area a', function () {
    $('.search-btn-area a').removeClass('current');
    $(this).addClass('current');
});

//点击搜索按钮
$(document).on('click', '#search', function () {
    $('.search-btn-area a').each(function () {
        if ($(this).hasClass('current')) {
            _text = $(this).html();
        }
    });
    keyword = $('.search-input #keyword').val();
    if (_text == "内容") {
        $('.interest-list').html("");
        $('.pageNum').remove();
        $('.page_all').html("<div class='pageNum'></div>");
        $('.interest-list').removeClass('search-user-list');
        $('.interest-list').addClass('search-thread-list');
        data_motocycle = {
            page: 1,
            keywords: keyword,
            rows: 20
        };
        searchkey_content();
        page_show();
    } else if (_text == "用户") {
        $('.interest-list').html("");
        $('.pageNum').remove();
        $('.page_all').html("<div class='pageNum'></div>");
        $('.interest-list').removeClass('search-thread-list');
        $('.interest-list').addClass('search-user-list');
        data_motocycle = {
            user_id: user_id,
            nickname: keyword,
            page: 1,
            rows: 10
        };
        searchkey_user();
        page_show();
    }
});


//搜索内容
function searchkey_content() {
    $.ajax({
        type: 'get',
        url: url_link + "articles",
        async: false,
        data: data_motocycle,
        success: function (data) {
            try {
                var obj = JSON.parse(data);
                var discover_theme_info = "";
                total = obj.data.total;
                if (obj.code == 0) {
                    if (obj.data.total != 0) {
                        $.each(obj.data.rows, function (i, item) {
                            discover_theme_info += "<li data-article='" + item.article_id + "'>" +
                                "<div class='img-box'>" +
                                "<div class='interest-label'>" +
                                "<div class='label'>" +
                                "<a href='discover_theme.html?mototype_id=" + item.mototype_id + "' target='_blank'>" + item.mototype_name + "</a></div>" +
                                "</div>" +
                                "<a href='publish/show.html?article_id=" + item.article_id + "' target='_blank'>"
                            if (item.cover) {
                                discover_theme_info += "<img src='" + item.cover + "'>";
                            } else {
                                var _content = item.content.split("$$##MotorTravel##$$");
                                var _count = 0;
                                for (var _a = 0; _a < _content.length; _a++) {
                                    var m = _content[_a].substring(0, 13);
                                    if (m == "$$##Image##$$") {
                                        //有图
                                        _count = 1;
                                        var _content_image_description = _content[_a].split("$$##ImageDescription##$$");
                                        var _content_image_url = _content_image_description[0].split("$$##ImageInfo##$$");
                                        discover_theme_info += "<img src=" + _content_image_url[0].substring(13, _content_image_url[0].length) + ">"
                                        break;
                                    }
                                }
                                //纯文字,没有图的情况下,使用备用图片
                                if (_count == 0) {
                                    discover_theme_info += "<img src='../image/base/none_img.png' style='width: 233px;height: 115px;'>";
                                }
                                ;
                            }

                            discover_theme_info += "</a>" +
                                "<div class='about-count'>" +
                                "<span class='click'><i></i>" + item.see_count + "</span>" +
                                "<span class='comment'><i></i>" + item.comment_count + "</span>" +
                                "<span class='praise'><i></i>" + item.gift_count + "</span>" +
                                "</div>" +
                                "</div>" +
                                "<h4>" +
                                "<a href='publish/show.html?article_id=" + item.article_id + "' target='_blank' style='line-height:14px'>";

                            if (item.is_nice == 1) {
                                discover_theme_info += "<i class='essence'></i>"
                            }
                            discover_theme_info += item.title + "</a>" +
                                "</h4>" +
                                "<div class='user-info'>" +
                                "<a href='lookat/article.html?user_id=" + item.user_id + "' target='_blank' class='user'><i>"
                            if (item.user.avatar) {
                                discover_theme_info += "<img src=" + item.user.avatar + " style='width: 25px;height: 25px;'>"
                            } else {
                                discover_theme_info += "<img src='../image/base/none.png' style='width: 25px;height: 25px;'>"
                            }

                            discover_theme_info += "</i><em>" + item.user.nickname + "</em></a>";
                            //判断是否是今日更新
                            var updated_time = item.updatedDate.substring(0, 10);
                            if (mytime == updated_time) {
                                item.updatedDate = "今天   " + item.updatedDate.substring(11, item.updatedDate.length)
                            }
                            discover_theme_info += "<span class='pub-time'>" + item.updatedDate + "</span></div></li>";
                        });
                    } else {
                        if ($('.interest-list li').length == 0) {
                            discover_theme_info += "<h2 class='none_info'>没有找到您搜索的内容，换个词试试</h2>";
                            $('.pageNum').hide()
                        }
                    }
                    $('.interest-list').html(discover_theme_info)
                }
            } catch (e) {

            }
        }

    })
}

//搜索用户
function searchkey_user() {
    $.ajax({
        type: 'get',
        url: url_link + "users",
        async: false,
        data: data_motocycle,
        success: function (data) {
            try {
                var obj = JSON.parse(data);
                var discover_user_info = "";
                total = obj.data.total;
                if (obj.code == 0) {
                    if (obj.data.total != 0) {
                        $.each(obj.data.rows, function (i, item) {
                            discover_user_info += "<li class='myfriend'>" +
                                "<dl>" +
                                "<dt>" +
                                "<a href='lookat/article.html?user_id=" + item.user_id + "' target='_blank' class='headpic'>"
                            if (item.avatar) {
                                discover_user_info += "<img src=" + item.avatar + " style='width: 90px;height: 90px;' >"
                            } else {
                                discover_user_info += "<img src='../image/base/none.png' style='width: 90px;height: 90px;' >"
                            }
                            discover_user_info += "</a>";
                            if (item.sex == 1) {
                                discover_user_info += "<i class='male-mid'></i>"
                            } else {
                                discover_user_info += "<i class='female-mid'></i>"
                            }
                            discover_user_info += "</dt>" +
                                "<dd class='friend-info'>" +
                                "<h5>" +
                                "<a href='lookat/article.html?user_id=" + item.user_id + "' target='_blank'>" + item.nickname +
                                "<b class='level'>LV." + item.level + "</b></a></h5>" +
                                "<ul class='friend-info-count'>" +
                                "<li><span>关注</span><em>" + item.follow_count + "</em></li>" +
                                "<span class='line'></span>" +
                                "<li><span>粉丝</span><em>" + item.fans_count + "</em></li>" +
                                "<span class='line'></span>" +
                                "<li><span>作品</span><em>" + item.article_count + "</em></li>" +
                                "</ul>" +
                                "<p class='signature'>个性签名：" + item.sign + "</p>" +
                                "</dd>" +
                                "<dd class='button-area'>";
                            if (user_id == item.user_id) {
                                discover_user_info += " <a href='center/home.html' target='_blank' class='white-button user-home-btn'><i></i>个人中心</a>"
                            } else {
                                if (item.has_follow == 1) {
                                    //已关注
                                    discover_user_info += "<a href='javascript:;' class='white-button un-follow-btn' id='pay_attention_button' style='margin: 0 10px;'><i></i>取消关注</a>"
                                } else {
                                    //未关注
                                    discover_user_info += "<a href='javascript:;' class='white-button follow-btn' id='pay_attention_button' style='margin: 0 10px;'><i></i>关注</a>"
                                }
                                discover_user_info += "<a href='javascript:;'  class='white-button private-btn message-button' id='private_letter_button'><i></i>发私信</a></dd> </dl></li>"
                            }
                        });
                    } else {
                        if ($('.interest-list li').length == 0) {
                            discover_user_info += "<div class='search-user-list'><h2 class='none_info'>没有找到您搜索的内容，换个词试试</h2></div> ";
                            $('.pageNum').hide()
                        }
                    }
                    $('.interest-list').html(discover_user_info)
                }
            } catch (e) {

            }
        }

    })
}

//分页
function page_show() {
    //分页
    var _pagenum = Math.ceil(total / data_motocycle.rows);
    if (_pagenum > 1) {
        //需要分页展示
        $('.pageNum').show();
        $(".pageNum").createPage({
            pageCount: _pagenum,
            current: data_motocycle.page,
            backFn: function (p) {
                //单击回调方法，p是当前页码
                data_motocycle.page = p;
                if (_text == "内容") {
                    searchkey_content();
                } else if (_text == "用户") {
                    searchkey_user();
                }
            }
        });
    } else {
        $('.pageNum').hide();
    }
}

